using System;
using Microsoft.CSharp;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Runtime.Remoting;

namespace Thinxoft.HttpLib
{
    

	/// <summary>
	/// Summary description for Interpreter.
	/// </summary>
	public class Interpreter
	{
		StringBuilder code = new StringBuilder(), errorResponse = new StringBuilder();
		HttpResource xfile;
		HttpRequest request;
       
        
		

		public Interpreter(ref HttpResource resource,HttpRequest request)
		{		
			this.request = request;			
			xfile  = resource;			
		}       

        public HttpResponse Interprete()
		{
            
			if(xfile.Compiled)
			{
                if (xfile.HasPage)
                {
                    try
                    {
                       HttpResponse resp = new HttpResponse(xfile.ServerInfo, request.Cookies);
                       xfile.Page.LoadContext(this.request,xfile.ServerInfo,resp);
                       //xfile.Page.LoadContext(this.request);
                        return  (HttpResponse)xfile.Page.Execute();
                        //return resp;
                      //  int g = this.GetHashCode();
                      //  return new HttpResponse("Has Page OK fast [" + g + "]","200 OK",xfile.ServerInfo);
                        
                      //  return (HttpResponse)xfile.Page.Execute();
                    }
                    catch (RemotingException rem)
                    {
                        //refreshes after long wait
                        return ExecuteTempAssembly();
                    }
                }
                else
                {
                    return ExecuteTempAssembly();
                    //int g = this.GetHashCode();
                    //return new HttpResponse("Exec Assembly OK fast [" + g + "]", "200 OK", xfile.ServerInfo);
                   // return
                }
			}
			else
			{
				return new HttpResponse("The specified file" + xfile.FileName + " could not be dynamically interpreted","200 OK",xfile.ServerInfo);
			}
		}

		public HttpResponse ExecuteTempAssembly()
		{

            //AppDomain domain = xfile.ServerInfo.ApplicationDomain;// AppDomain.CreateDomain("ExecContext[" + Guid.NewGuid().ToString() + "]");

            string typeName  = "Thinxoft.HttpLib." + xfile.FileNameWithoutExtension;
            string dll = "xfiles/" + xfile.FileNameWithoutExtension + ".dll";
            AssemblyName assemblyName = AssemblyName.GetAssemblyName(dll);
            ObjectHandle oh = xfile.ServerInfo.ApplicationDomain.CreateInstanceFrom(dll, typeName);           
			XmlPage page = (XmlPage)oh.Unwrap();            
            xfile.SetPageInstance(ref page);
			

			HttpResponse resp = new HttpResponse(xfile.ServerInfo,request.Cookies);
            page.LoadContext(this.request,xfile.ServerInfo,resp);

            HttpResponse response = (HttpResponse)page.Execute();	
			
			//AppDomain.Unload(domain); //unload domain
			
			return response;
			
		}

		
	}


}
